package com.ocom.payconfig.controller;


import com.alibaba.fastjson.JSON;
import com.ocom.common.constant.PayConfigUrl;
import com.ocom.common.entity.card.EntityXfAccountType;
import com.ocom.common.entity.card.EntityXfRecordOrg;
import com.ocom.common.entity.human.EntityPersonnelInfo;
import com.ocom.common.request.exposeapi.LargeScreenRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.payconfig.client.XfRecordFeign;
import com.ocom.payconfig.service.PersonnelInfoService;
import com.ocom.payconfig.service.XfAccountTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class LargeScreenController {


    @Autowired
    PersonnelInfoService personnelInfoService;

    @Autowired
    XfAccountTypeService xfAccountTypeService;

//    @Autowired
//    XfMealInfoService xfMealInfoService;

    @Autowired
    XfRecordFeign xfRecordFeign;


    @PostMapping(PayConfigUrl.Url_totalNumberPerson)
    public Result totalNumberPerson(@RequestBody LargeScreenRequest request) {
        List<EntityPersonnelInfo> entityPersonnelInfos = personnelInfoService.totalNumberPerson(Long.valueOf(request.getCompanyId()));
        List<EntityXfAccountType> xfAccountTypes = xfAccountTypeService.GetXFAccountType(Long.valueOf(request.getCompanyId()),0);


        List<Map> userList = new ArrayList<>();
        List<Map> typeList = new ArrayList<>();

        if (entityPersonnelInfos.size() > 0) {
            for (EntityPersonnelInfo entityPersonnelInfo : entityPersonnelInfos) {
                Map<String, Object> resMap = new HashMap<>();
                if (entityPersonnelInfo.getPType() == null) {
                    continue;
                }
                resMap.put("accTypeId", entityPersonnelInfo.getPType());
                resMap.put("perNums", entityPersonnelInfo.getCardState());
                userList.add(resMap);
            }
        }

        if (xfAccountTypes.size() > 0) {
            for (EntityXfAccountType entityXfAccountType : xfAccountTypes) {
                Map<String, Object> resMap = new HashMap<>();
                resMap.put("accTypeId", entityXfAccountType.getAccTypeId());
                resMap.put("accTypeName", entityXfAccountType.getAccTypeName());
                typeList.add(resMap);
            }
        }

        Map<String, Object> resMap = new HashMap<>();

        resMap.put("user", userList);
        resMap.put("accType", typeList);

        return ResultUtil.success(resMap);
    }


    @PostMapping(PayConfigUrl.Url_yesterdayStudentMealRate)
    public Result yesterdayStudentMealRate(@RequestBody LargeScreenRequest request) {

//        List<EntityXfMealInfo> entityXfMealInfos = xfMealInfoService.GetMealNameInfo(null,Long.valueOf(request.getCompanyId()),null,null);

        List<EntityPersonnelInfo> entityPersonnelInfos = personnelInfoService.totalNumberPerson(Long.valueOf(request.getCompanyId()));

        Integer accTypeId = request.getParam().getInteger("accTypeId");

        /**
         * breakfast	string	Y	早餐就餐率,如：89.00%
         * lunch	string	Y	午餐就餐率,如：89.00%
         * dinner	string	Y	晚餐就餐率,如：89.00%
         * breakfast_per_person	float	Y	早餐人均多少元
         * lunch_per_person	float	Y	午餐人均多少元
         * dinner_per_person	float	Y	晚餐人均多少元
         */


        Result result = xfRecordFeign.yesterdayStudentMealRate(request);

        /**
         * breakfast	string	Y	早餐就餐率,如：89.00%
         * lunch	string	Y	午餐就餐率,如：89.00%
         * dinner	string	Y	晚餐就餐率,如：89.00%
         * breakfast_per_person	float	Y	早餐人均多少元
         * lunch_per_person	float	Y	午餐人均多少元
         * dinner_per_person	float	Y	晚餐人均多少元
         */




        Integer perNums = 0 ;
        if (accTypeId == null || accTypeId <= 0) {
            List<Integer> sumPers = entityPersonnelInfos.stream().map(EntityPersonnelInfo::getCardState).collect(Collectors.toList());
            perNums = sumPers.stream().mapToInt(Integer::intValue).sum();
        }else{
            for (EntityPersonnelInfo entityPersonnelInfo:entityPersonnelInfos){
                if(entityPersonnelInfo.getPType().equals(accTypeId.longValue())){
                    perNums = entityPersonnelInfo.getCardState();
                }
                break;
            }
        }
        if(perNums == 0)   perNums = 1;

        Map<String,Object> resMap =new HashMap<>();
        resMap.put("breakfast","0");
        resMap.put("lunch","0");
        resMap.put("dinner","0");
        resMap.put("breakfast_per_person","0");
        resMap.put("lunch_per_person","0");
        resMap.put("dinner_per_person","0");

        BigDecimal perNumsBig  =  new BigDecimal(perNums);

        if(result.getCode() == 200 ){
            List<EntityXfRecordOrg> xfRecordOrgList = JSON.parseArray(JSON.toJSONString(result.getData()),EntityXfRecordOrg.class);
            for (EntityXfRecordOrg entityXfRecordOrg:xfRecordOrgList){
                BigDecimal perNow  =  new BigDecimal(entityXfRecordOrg.getConsState());
                BigDecimal moneyNow  =  new BigDecimal(entityXfRecordOrg.getConsType());

                DecimalFormat df =new DecimalFormat("0.00");

                String df1 = df.format(perNow.divide(perNumsBig ,4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)));
                String df2 = df.format(moneyNow.divide(perNow.multiply(new BigDecimal(100)),2, BigDecimal.ROUND_HALF_UP));


                if(Float.valueOf(df1)>100){
                    df1 ="100";
                }

                if(entityXfRecordOrg.getMealNo().equals("1")){
                    resMap.put("breakfast",df1);
                    resMap.put("breakfast_per_person",df2);
                }
                if(entityXfRecordOrg.getMealNo().equals("2")){
                    resMap.put("lunch",df1);
                    resMap.put("lunch_per_person",df2);
                }
                if(entityXfRecordOrg.getMealNo().equals("3")){
                    resMap.put("dinner",df1);
                    resMap.put("dinner_per_person",df2);
                }
            }
        }

        return ResultUtil.success(resMap);
    }


}
